package com.libs.sys.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.libs.sys.model.Seat;
import com.libs.sys.model.User;
import com.libs.sys.model.Userseat;
import com.libs.sys.service.LibService;
import com.libs.sys.service.SeatService;
import com.libs.sys.service.UserseatService;
import com.libs.utils.HttpClientConn;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.impl.client.CloseableHttpClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * InnoDB free: 10240 kB 前端控制器
 * </p>
 *
 * @author SR.Y
 * @since 2021-08-11
 */
@Slf4j
@RestController
@RequestMapping("/userseat")
public class UserseatController {

    @Autowired
    private UserseatService userseatService;
    @Autowired
    private SeatService seatService;
    private LibService libService;
    @RequestMapping(value = "/aou", method = RequestMethod.GET)
    @ResponseBody
    public boolean aou(Integer id, String uid, String pwd, String seatid) {
        Userseat userseat = new Userseat(uid, pwd, seatid);
        if (id == -1) {
            return add(userseat);
        } else {
            return update(id, userseat);
        }
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public boolean add(@RequestBody Userseat userseat) {
        String sid = userseat.getSeatid();
        Seat seat = seatService.selectoneseat(sid);
        userseat.setSeatid(seat.getSeatid());
        if (!userseatService.isHaveSeat(userseat)) {
            log.info("{}添加成功", sid);
            return userseatService.add(userseat);
        } else {
            return false;
        }


    }

    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @ResponseBody
    public boolean update(@PathVariable Integer id, @RequestBody Userseat userseat) {
        userseat.setId(id);
        String sid = userseat.getSeatid();
        Seat seat = seatService.selectoneseat(sid);
        userseat.setSeatid(seat.getSeatid());
        if (!userseatService.isHaveSeat(userseat)) {
            log.info("{}修改座位为{}", userseat.getUid(), sid);
            return userseatService.updateById(userseat);
        } else {
            log.info("{}修改座位失败", userseat.getUid());
            return false;
        }
    }

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public Page<Userseat> list() {
        return userseatService.list(0, 500);
    }

    @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
    @ResponseBody
    public Userseat getOne(@PathVariable Integer id) {
        return userseatService.getOneById(id);
    }

    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    @ResponseBody
    public boolean delete(@PathVariable Integer id) {
        List<Integer> idList = new ArrayList<>();
        idList.add(id);
        return userseatService.delete(idList);
    }

    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseBody
    public Page<Userseat> list(@RequestBody Userseat userseat,
                               @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                               @RequestParam(value = "pageNum", defaultValue = "0") Integer pageNum) {
// public Page<Userseat> list(@RequestBody Userseat userseat,
//                              @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
//                              @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {

        String sid = seatService.selectoneseat(userseat.getSeatid()).getSeatid();
        System.out.println(sid);
        userseat.setSeatid(sid);
        return userseatService.list(userseat, pageNum, pageSize);

    }

    @RequestMapping(value = "/user", method = RequestMethod.GET)
    @ResponseBody
    public List<User> userList() throws Exception {
        final List<Userseat> list = userseatService.list();
        List<User> userList = new ArrayList<>();
        for (Userseat u :
                list) {
            System.out.println(u.getUid());
            String name = libService.login(u, HttpClientConn.getHttpClient());
            Integer sid = seatService.getseatBySeatId(u.getSeatid()).getId();
            userList.add(new User(u.getUid(), name, sid));
        }
        return userList;
    }

    @RequestMapping(value = "/getUser", method = RequestMethod.GET)
    @ResponseBody
    public List<Integer> getList() throws Exception {
        final List<Userseat> list = userseatService.list();
        List<Integer> idList = new ArrayList<>();
        for (Userseat u :
                list) {
            Integer sid = seatService.getseatBySeatId(u.getSeatid()).getId();
            idList.add(sid);
        }
        return idList;
    }

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @ResponseBody
    public List<Integer> login(@RequestParam String uid,
                               @RequestParam String pwd) {
        log.info("【修改座位】{}==登录", uid);
        final Userseat login = userseatService.login(uid, pwd);
        List<Integer> list = new ArrayList<>();
        if (login != null) {
            Integer sid = seatService.getseatBySeatId(login.getSeatid()).getId();
            list.add(login.getId());
            list.add(sid);
        }
        return list;

    }
    @RequestMapping(value = "/test", method = RequestMethod.GET)
    @ResponseBody
    public String test() throws Exception {
        return test(new Integer("1"));
    }

    @RequestMapping(value = "/test/{id}", method = RequestMethod.GET)
    @ResponseBody
    public String test(@PathVariable Integer id) throws Exception {
        if(id==null){
            id=1;
        }
        Userseat userseat = getOne(id);
        CloseableHttpClient httpClient = HttpClientConn.getHttpClient();
        LibService libService = new com.libs.sys.service.impl.LibServiceImpl();
        String name = libService.login(userseat, httpClient);
        char flag = libService.setSeat(userseat, httpClient);
        boolean isSeat = libService.signSeat(userseat, httpClient);
        String res = name + flag + isSeat;
        log.info("{}",res);
        return res;
    }
}

